package animation9.view.xiaomage.com.viewanimator9

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val backgroundColorObjectAnimator=
                ObjectAnimator.ofInt(view1,"backgroundColor",
                        0xffff00ff.toInt(),
                        0xffffff00.toInt(),
                        0xffff00ff.toInt())
        val translateY1ObjectAnimator=
                ObjectAnimator.ofFloat(view1,"translationY",
                        0f,300f,0f)
        val translateY2ObjectAnimator=
                ObjectAnimator.ofFloat(view2,"translationY",
                        0f,300f,0f)
        bt_sequentially.setOnClickListener{
            val animatorSet=AnimatorSet()
            animatorSet.playSequentially(backgroundColorObjectAnimator,translateY1ObjectAnimator,
                    translateY2ObjectAnimator)
            animatorSet.duration=5000
            animatorSet.start()
        }

        bt_together.setOnClickListener{
            val togetherAnimatorSet=AnimatorSet()
            togetherAnimatorSet.playTogether(backgroundColorObjectAnimator,
                    translateY1ObjectAnimator,translateY2ObjectAnimator)
            togetherAnimatorSet.duration=5000
            togetherAnimatorSet.start()
        }
        bt_builder.setOnClickListener{
            backgroundColorObjectAnimator.startDelay=5000
            val animatorSet=AnimatorSet()
            val animatorSetBuilder=animatorSet.play(backgroundColorObjectAnimator)
            animatorSet.duration=5000
            //如果在这里就设置对象，下面再设置动画还是会作用在view1上
            //animatorSet.setTarget(view2)

            animatorSetBuilder.with(translateY1ObjectAnimator)
            animatorSet.setTarget(view2)
            animatorSet.start()
        }
    }
}
